package com.hcb.tonguetiphealth.manager;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.hcb.tonguetiphealth.model.Province;

import android.util.Xml;

public class XmlParserManager {
	public static List<Province> getProvinces(InputStream is) throws XmlPullParserException, IOException, Exception {
		List<Province> list = new ArrayList<Province>();
		Province province = null;
		String tag;
		
		XmlPullParser parser = Xml.newPullParser();
        parser.setInput(is, "UTF-8");
        int type = parser.getEventType(); 
        while (type != XmlPullParser.END_DOCUMENT) {
            switch (type) {
            case XmlPullParser.START_DOCUMENT:
                break;
            case XmlPullParser.START_TAG:
                tag = parser.getName();
                type = parser.next();
                if("province".equals(tag)) {
                	province = new Province();
                } else if ("id".equals(tag)) {
                    province.setId(parser.getText());
                } else if ("hasCitys".equals(tag)) {
                	String s = parser.getText();
                	if("1".equals(s)) {
                		province.setHasCitys(true);
                	}
                } else if ("name".equals(tag)) {
                	province.setName(parser.getText());
                    list.add(province);
                }
                break;
            case XmlPullParser.END_TAG:
                break;
            case XmlPullParser.END_DOCUMENT:
                break;
            }
            type = parser.next();
        }
        is.close();
        
		return list;
	}
	public static List<String> getCitys(InputStream is) throws XmlPullParserException, IOException, Exception {
		List<String> list = new ArrayList<String>();
		String tag;
		
		XmlPullParser parser = Xml.newPullParser();
        parser.setInput(is, "UTF-8");
        int type = parser.getEventType(); 
        while (type != XmlPullParser.END_DOCUMENT) {
            switch (type) {
            case XmlPullParser.START_DOCUMENT:
                break;
            case XmlPullParser.START_TAG:
                tag = parser.getName();
                if ("name".equals(tag)) {
                    type = parser.next();
                    list.add(parser.getText());
                }
                break;
            case XmlPullParser.END_TAG:
                break;
            case XmlPullParser.END_DOCUMENT:
                break;
            }
            type = parser.next();
        }
        is.close();
        
		return list;
	}
}
